package com.itcast.main.service.system.impl;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itcast.main.dao.system.RoleRepository;
import com.itcast.main.dao.system.UserRepository;
import com.itcast.main.domain.system.Role;
import com.itcast.main.domain.system.User;
import com.itcast.main.service.system.UserService;
@Service
@Transactional
public class UserServiceImpl implements UserService{
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private RoleRepository roleRepository;
	
	@Override
	public User findByUsername(String username) {
		return userRepository.findByUsername(username);
	}


	@Override
	public List<User> findAll() {
		return userRepository.findAll();
	}


	@Override
	public void saveUser(User model, String[] roleIds) {
		//保存用户
		userRepository.save(model);
		//授权角色
		if (roleIds != null) {
			for (String roleId : roleIds) {
				Role role = roleRepository.findOne(Integer.parseInt(roleId));
				model.getRoles().add(role);
			}
		}
	}

}
